package com.cacshanghai.cac_oa_client.widget;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.codetroopers.betterpickers.radialtimepicker.RadialTimePickerDialogFragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by geyh on 2016/6/2.
 */
public class TimePickView extends TextView {
    public TimePickView(Context context) {
        super(context);
        init(context);
    }

    public TimePickView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TimePickView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                RadialTimePickerDialogFragment rtpd = new RadialTimePickerDialogFragment().setThemeLight().setOnTimeSetListener(new RadialTimePickerDialogFragment.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(RadialTimePickerDialogFragment dialog, int hourOfDay, int minute) {
                        try {
                            Date time = new SimpleDateFormat("H:m").parse(hourOfDay + ":" + minute);
                            ((TextView) v).setText(new SimpleDateFormat("HH:mm").format(time));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                });
                rtpd.show(((FragmentActivity) context).getSupportFragmentManager(), "fragment_date_picker_name");
            }
        });
    }

}
